package cn.cosmosx.base.helper;

import cn.cosmosx.base.util.ObjectsUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * application上下文bean实例帮助类
 *
 * @author gengzhy
 */
@Component
public final class SpringContextHolder implements BeanFactoryPostProcessor, ApplicationContextAware {
    /**
     * Spring应用上下文环境
     */
    private static ConfigurableListableBeanFactory beanFactory;
    private static ApplicationContext applicationContext;

    /**
     * 根据Bean Type 从Spring容器获取Bean
     */
    public static <T> T getBean(Class<T> beanClazz) {
        return beanFactory.getBean(beanClazz);
    }

    /**
     * 根据Bean Name 从Spring容器获取Bean
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        return (T) beanFactory.getBean(beanName);
    }

    /**
     * Spring容器是否存在Bean
     */
    public static boolean containsBean(String beanName) {
        return beanFactory.containsBean(beanName);
    }

    /**
     * 是否单例
     */
    public static boolean isSingleton(String beanName) throws NoSuchBeanDefinitionException {
        return beanFactory.isSingleton(beanName);
    }

    /**
     * 根据Bean Name获取其类型
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getType(name);
    }

    /**
     * 如果给定的bean名字在bean定义中有别名，则返回这些别名
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getAliases(name);
    }

    /**
     * 获取当前aop代理对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T getCurrentAopProxy() {
        return (T) AopContext.currentProxy();
    }

    /**
     * 获取当前的环境配置，无配置返回null
     *
     * @return 当前的环境配置
     */
    public static String[] getActiveProfiles() {
        return applicationContext.getEnvironment().getActiveProfiles();
    }

    /**
     * 获取当前的环境配置，当有多个环境配置时，只获取第一个
     *
     * @return 当前的环境配置
     */
    public static String getActiveProfile() {
        final String[] activeProfiles = getActiveProfiles();
        return ObjectsUtil.isNotNull(activeProfiles) ? activeProfiles[0] : null;
    }

    /**
     * 获取配置文件中的值
     *
     * @param key  配置文件的key
     * @param type 返回类型
     * @return 当前的配置文件的值
     */
    public static <T> T getProperty(String key, Class<T> type) {
        return applicationContext.getEnvironment().getRequiredProperty(key, type);
    }

    /**
     * 获取配置文件中的值
     *
     * @param key 配置文件的key
     * @return 当前的配置文件的值
     */
    public static String getProperty(String key) {
        return getProperty(key, String.class);
    }

    @Override
    public void postProcessBeanFactory(@NotNull ConfigurableListableBeanFactory beanFactory) throws BeansException {
        SpringContextHolder.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }
}
